#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
using std::string;

void test0(){
    string str1;
    cout << str1 << endl;

    string str2("hello");
    cout << str2 << endl;

    string str3("hello",3);//hel
    cout << str3 << endl;

    string str4(str2);//hello
    cout << str4 << endl;

    string str5(str2,1);//ello
    cout << str5 << endl;

    string str6(str2,1,3);//ell
    cout << str6 << endl;

    string str7(5,'a');//aaaaa
    cout << str7 << endl;
}

void test1(){
    string str1("hello");
    //string可以视为是存放char型元素的容器


    //迭代器是广义的指针
    //begin函数返回值为首迭代器
    /* char * it = str1.begin(); */
    string::iterator it = str1.begin();
    it = it + 4;
    cout << *it << endl;
    /* it++; */
    /* cout << *it << endl; */
    /* it++; */
    /* cout << *it << endl; */
    /* it++; */
    /* cout << *it << endl; */

    //end函数返回值为尾后迭代器
    /* string::iterator it2 = str1.end(); */
    //auto关键字可以自动推导类型
    auto it2 = str1.end();
    it2--;
    cout << *it2 << endl;
}

void test2(){
    string str1("hello");
    auto it = str1.begin();
    auto it2 = str1.end() - 1;
    string str2(it,it2);
    cout << str2 << endl;

    string str3 = str1 + ',' + str2 + 'o' + ',' + "world";
    cout << str3 << endl;
}

//string的常用操作
void test3(){
    string str1("hello");
    const char * pstr = "world";
    printf("%s\n",str1.c_str());
    printf("%s\n",pstr);

    cout << "is str1 empty?" << str1.empty() << endl;
    cout << "str1's size:" << str1.size() << endl;

    str1.push_back('!');
    cout << str1 << endl;

    string str2("world");
    str1.append(str2,2,3);
    cout << str1 << endl;

    //查找子串
    size_t pos = str1.find("ll",3,1);
    cout << pos << endl;

}

int main(void){
    test3();
    return 0;
}
